#include "pagewidthset.h"
#include "jsondeploy.h"

PageWidthSet::PageWidthSet(float width, QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	JsonDeploy::init();

	if (qAbs(width - 21) <= 0.1f)
	{
		ui.radioButtonA4->setChecked(true);
	}
	else if (qAbs(width - 29.7f) <= 0.1f)
	{
		ui.radioButtonA3->setChecked(true);
	}
	else if (qAbs(width - 59.4f) <= 0.1f)
	{
		ui.radioButtonA2->setChecked(true);
	}
	else
	{
		ui.radioButtonUserDefine->setChecked(true);
		ui.spinBox->setValue(int(width));
	}

	m_diaryWidth = JsonDeploy::getKeyValueFromNumSets(DIARY_DEFAULT_WIDTH);
	ui.diaryPageWidthLe->setText(QString::number(m_diaryWidth));

	m_articleWidth = JsonDeploy::getKeyValueFromNumSets(ARTICLE_DEFAULT_WIDTH);
	ui.articlePageWidthLe->setText(QString::number(m_articleWidth));
}

PageWidthSet::~PageWidthSet()
{
	JsonDeploy::close();
}


void PageWidthSet::slot_ok()
{
	if (ui.radioButtonA4->isChecked())
	{
		emit pageWidthChange(21.0f);
	}
	else if (ui.radioButtonA3->isChecked())
	{
		emit pageWidthChange(29.7f);
	}
	else if (ui.radioButtonA2->isChecked())
	{
		emit pageWidthChange(59.4f);
	}
	else if (ui.radioButtonUserDefine->isChecked())
	{
		emit pageWidthChange(float(ui.spinBox->value()));
	}

	int newDiaryWidth = ui.diaryPageWidthLe->text().toInt();
	int articleWidth = ui.articlePageWidthLe->text().toInt();

	bool defaultChange = false;

	if (m_diaryWidth != newDiaryWidth)
	{
		JsonDeploy::updataKeyValueFromNumSets(DIARY_DEFAULT_WIDTH, newDiaryWidth);
		defaultChange = true;
}
	if (m_articleWidth != articleWidth)
	{
		JsonDeploy::updataKeyValueFromNumSets(ARTICLE_DEFAULT_WIDTH, articleWidth);
		defaultChange = true;
	}

	if (defaultChange)
	{
		emit pageDefaultChange(newDiaryWidth, articleWidth);
}
}
